package com.ziqing.apeletnews.adapter;

import android.content.Context;
import android.support.annotation.NonNull;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ziqing.apeletnews.R;
import com.ziqing.apeletnews.base.BaseAdapter;
import com.ziqing.apeletnews.base.BaseViewHolder;
import com.ziqing.apeletnews.bean.ArticleBean;

import java.util.List;


/**
 * Created by WT on 2018/4/13.
 */

public class ArticleAdapter extends RecyclerView.Adapter {
    //定义三种常量  表示三种条目类型
    public static final int TYPE_PULL_IMAGE = 0;
    public static final int TYPE_RIGHT_IMAGE = 1;
    private List<ArticleBean> mData;
    private Context mContext;
    public ArticleAdapter(List<ArticleBean> data,Context mContext) {
        this.mData = data;
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建不同的 ViewHolder
        View view;
        //根据viewtype来创建条目

        if (viewType == TYPE_PULL_IMAGE) {
//            view =View.inflate(parent.getContext(), R.layout.item_recycle_article_one,parent);
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_article_one,parent,false);
            return new PullImageHolder(view);
        }else{
//            view =View.inflate(parent.getContext(),R.layout.item_recycle_article_two,null);
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_article_two,parent,false);
            return new RightImageHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == TYPE_PULL_IMAGE){
            ImageView pic = holder.itemView.findViewById(R.id.iv_article_one);
//            RoundedImageView avatar =  holder.itemView.findViewById(R.id.iv_head_icon);
            Glide.with(mContext).load(mData.get(position).getArticleOne()).into(pic);
        }else if(holder.getItemViewType() == TYPE_RIGHT_IMAGE){
            ImageView pic = holder.itemView.findViewById(R.id.iv_article_two);
            Glide.with(mContext).load(mData.get(position).getArticleTwo()).into(pic);
        }
    }

    @Override
    public int getItemViewType(int position) {
        ArticleBean moreTypeBean = mData.get(position);
        if (moreTypeBean.getType() == 0) {
            return TYPE_PULL_IMAGE;
        }else{
            return TYPE_RIGHT_IMAGE;
        }


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 创建三种ViewHolder
     */
    private class PullImageHolder extends RecyclerView.ViewHolder {

        public PullImageHolder(View itemView) {
            super(itemView);
        }
    }

    private class RightImageHolder extends RecyclerView.ViewHolder {

        public RightImageHolder(View itemView) {
            super(itemView);
        }
    }


}
